<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.min.js"></script>
  </head>
  <body>
    <div id="app">
      <ul>
        <li v-for="item in arr">
          <input type="checkbox" v-model="item.check" />
          商品名称：{{item.title}} 商品价格：{{item.price}}
          <button @click="pre(item)">减</button>
          商品数量：{{item.count}}
          <button @click="add(item)">加</button>
          商品小计：{{item.price*item.count}}
        </li>
      </ul>

      <div>
        <p>全选 <input type="checkbox" v-model="allCheck" @change="changeFlag" /></p>
        <p>总数量：{{allCount}}</p>
        <p>总价格:{{allPrice}}</p>
      </div>
    </div>
    <script>
      new Vue({
        el: "#app",
        data: {
          allCheck: false,
          arr: [
            {
              title: "华为",
              price: 100,
              count: 0,
              check: false,
            },
            {
              title: "小米",
              price: 200,
              count: 0,
              check: false,
            },
            {
              title: "苹果",
              price: 300,
              count: 0,
              check: false,
            },
          ],
        },
        watch: {
          arr: {
            deep: true,
            handler(value) {
              this.allCheck = value.every((item) => item.check);
            },
          },
        },
        computed: {
          allCount() {
            return this.arr.reduce((next, prev) => {
              if (prev.check) {
                return next + prev.count;
              } else {
                return next;
              }
            }, 0);
          },
          allPrice() {
            return this.arr.reduce((next, prev) => {
              if (prev.check) {
                return next + prev.price * prev.count;
              } else {
                return next;
              }
            }, 0);
          },
        },
        methods: {
            add(item){
                item.count++
            },
            pre(item){
                item.count++
            },
            changeFlag() {
                    this.arr.forEach(item => {
                        item.check = this.allCheck
                    })
                }
        },
      });
    </script>
  </body>
</html>
